// 导入懒加载
import { lazy, Suspense } from 'react'
// 一级路由
var NotFound = lazy(() => import('../../views/NotFound'))
// 二级路由
var ExpenseManagement = lazy(() => import('../../views/Index/ExpenseManagement'))
// 三级路由
var Outpatientconsumption = lazy(() => import('../../views/Index/expense management/Outpatientconsumption'))
var Outpatientrecharge = lazy(() => import('../../views/Index/expense management\/Outpatientrecharge'))
var Inpatientconsumption = lazy(() => import('../../views/Index/expense management/Inpatientconsumption'))
var Inpatientrecharge = lazy(() => import('../../views/Index/expense management/Inpatientrecharge'))
var Refundrecord = lazy(() => import('../../views/Index/expense management/Refundrecord'))
var DetailsPage = lazy(() => import('../../component/liuhuan/DetailsPage'))

// 404
var NotFound = lazy(() => import('../../views/NotFound'))

// 高阶函数，判断权限
function AuthComponent({ children, permission }) {
    // 获取用户权限
    var permissions = localStorage.getItem('userinfo') ?
        JSON.parse(localStorage.getItem('userinfo')).role.permission : [];

    //判断当前用户是否有权限访问该路由对应的组件
    var res = permission && permissions.includes(permission)

    return (
        // 懒加载
        <Suspense fallback={<div>loading...</div>}>
            {
                // res为ture则可以访问组件，反之渲染404页面
                res ? children : <NotFound />
            }
        </Suspense>
    )
}

export default [//费用管理的二级路由
    {
        path: '/index/expensemanagement',
        element: <AuthComponent permission={13}><ExpenseManagement /></AuthComponent>,
        children: [
            {
                path: '/index/expensemanagement/outpatientconsumption',
                element: <AuthComponent permission={131}><Outpatientconsumption /></AuthComponent>,
            },
            {
                path: '/index/expensemanagement/outpatientrecharge',
                element: <AuthComponent permission={132}><Outpatientrecharge /></AuthComponent>,
            },
            {
                path: '/index/expensemanagement/inpatientconsumption',
                element: <AuthComponent permission={133}><Inpatientconsumption /></AuthComponent>,
            },
            {
                path: '/index/expensemanagement/inpatientrecharge',
                element: <AuthComponent permission={134}><Inpatientrecharge /></AuthComponent>,
            },
            {
                path: '/index/expensemanagement/refundrecord',
                element: <AuthComponent permission={135}><Refundrecord /></AuthComponent>,
            },
            {
                path: '/index/expensemanagement/detailsPage',
                element: <AuthComponent permission={13}><DetailsPage /></ AuthComponent>
            }
        ]
    },
]